package com.cyj.crm02.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyj.crm02.entity.EmployeeRole;
import com.cyj.crm02.entity.Role;
import com.cyj.crm02.entity.RoleDepartment;
import com.cyj.crm02.entity.RolePermission;
import com.cyj.crm02.mapper.RoleMapper;
import com.cyj.crm02.service.RoleDepartmentService;
import com.cyj.crm02.service.RolePermissionService;
import com.cyj.crm02.service.RoleService;
import com.cyj.crm02.utils.SecurityUtils;
import com.cyj.crm02.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleDepartmentService roleDepartmentService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public IPage<Role> selectRoleList(Page<Role> page) {
        return roleMapper.selectRoleList(page);
    }

    @Override
    public Integer addRoleDepartment(Role role, Integer departmentId) {
        System.out.println("添加方法的参数：role:"+role);
        System.out.println("添加方法的参数：departmentId:"+departmentId);

        //设置时间，idDel
        role.setCreateTime(StringUtils.getNowTime());

        int roleNum = roleMapper.insert(role);

        if (roleNum==1){
            //将departmentId和roleId一同插入到  角色部门关系表
            RoleDepartment roleDepartment  = new RoleDepartment(role.getRoleId(),departmentId);
            roleDepartment.setCreateTime(StringUtils.getNowTime());

            boolean flag = roleDepartmentService.save(roleDepartment);
            System.out.println("角色部门关系表插入："+flag);
        }


        return roleNum;
    }

    @Override
    public Integer addRolePermission(Role role, Integer permissionId) {
        System.out.println("添加方法的参数：role:"+role);
        System.out.println("添加方法的参数：permissionId:"+permissionId);


        //将permissionId和roleId一同插入到  角色权限关系表
        RolePermission rolePermission  = new RolePermission(role.getRoleId(),permissionId);
        rolePermission.setCreateTime(StringUtils.getNowTime());
        rolePermission.setUpdateTime(StringUtils.getNowTime());

        boolean flag = rolePermissionService.save(rolePermission);
        System.out.println("角色权限关系表插入："+flag);

        int roleNum = 0;
        if (flag==true){
            roleNum = 1;
        }

        return roleNum;
    }

    @Override
    public Integer update(Role role, Integer departmentId) {
        System.out.println("角色信息参数："+role);
        //将信息更新到数据库中（空的属性不修改）
        int result = roleMapper.updateById(role);

        RoleDepartment roleDepartment  = new RoleDepartment(role.getRoleId(),departmentId);

        AbstractWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id",role.getRoleId());
        roleDepartmentService.update(roleDepartment,wrapper);

        return result;
    }
}
